package com.budwk.app.wxprogram.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.wxprogram.models.Wx_event_schedule;
import com.budwk.app.wxprogram.services.WxEventScheduleService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/wxprogram/event/schedule")
@ApiDefinition(tag = "事件进度表")
@Slf4j
public class WxEventScheduleController{
    @Inject
    private WxEventScheduleService wxEventScheduleService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询事件进度表")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = Wx_event_schedule.class
    )
    @ApiResponses(
    implementation = Wx_event_schedule.class
    )
    @SaCheckPermission("wxprogram.event.schedule")
    public Result<?> list(@Param("..")Wx_event_schedule bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        
        if (bean.getEvent_id() !=null && Strings.isNotBlank(bean.getEvent_id().toString())) {
            cnd.and("event_id","=",bean.getEvent_id());
        }
        
        if (bean.getEvent_type() !=null && Strings.isNotBlank(bean.getEvent_type().toString())) {
            cnd.and("event_type","=",bean.getEvent_type());
        }
        
        if (bean.getEvent_time() !=null && Strings.isNotBlank(bean.getEvent_time().toString())) {
            cnd.and("event_time","=",bean.getEvent_time());
        }
        
        if (bean.getEvent_content() !=null && Strings.isNotBlank(bean.getEvent_content().toString())) {
            cnd.and("event_content","=",bean.getEvent_content());
        }
        
        if (bean.getEvent_remark() !=null && Strings.isNotBlank(bean.getEvent_remark().toString())) {
            cnd.and("event_remark","=",bean.getEvent_remark());
        }
        
        if (bean.getEvent_status() !=null && Strings.isNotBlank(bean.getEvent_status().toString())) {
            cnd.and("event_status","=",bean.getEvent_status());
        }
        
        return Result.data(wxEventScheduleService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增事件进度表")
    @ApiFormParams(
        implementation = Wx_event_schedule.class
    )
    @ApiResponses
    @SLog(tag = "新增事件进度表", value = "${args[0].id}")
    @SaCheckPermission("wxprogram.event.schedule.create")
    public Result<?> create(@Param("..")Wx_event_schedule wxEventSchedule, HttpServletRequest req) {
        wxEventSchedule.setCreatedBy(SecurityUtil.getUserId());
        wxEventScheduleService.insert(wxEventSchedule);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改事件进度表")
    @ApiFormParams(
        implementation = Wx_event_schedule.class
    )
    @ApiResponses
    @SLog(tag = "修改事件进度表", value = "${args[0].id}")
    @SaCheckPermission("wxprogram.event.schedule.update")
    public Result<?> update(@Param("..")Wx_event_schedule wxEventSchedule, HttpServletRequest req) {
        wxEventSchedule.setUpdatedBy(SecurityUtil.getUserId());
        wxEventScheduleService.updateIgnoreNull(wxEventSchedule);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取事件进度表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("wxprogram.event.schedule")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        Wx_event_schedule wxEventSchedule = wxEventScheduleService.fetch(id);
        if (wxEventSchedule == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wxEventSchedule);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除事件进度表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除事件进度表", value = "${args[0]}")
    @SaCheckPermission("wxprogram.event.schedule.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        Wx_event_schedule wxEventSchedule = wxEventScheduleService.fetch(id);
        if (wxEventSchedule == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxEventScheduleService.delete(id);
        req.setAttribute("_slog_msg", wxEventSchedule.getId());
        return Result.success();
    }
}
